#!/bin/sh

. "$HOME/.config/sh/functions"

white="#d3d7cf"
yellow="#db9d3b"
red="#892b30"
green="#1c9867"

render_acpi() {
	acpi=$(acpi | tail -n1)

	state=$(echo "$acpi" | cut -d ',' -f 1 | cut -d ':' -f 2 | tr -d ' ')
	load=$(echo "$acpi" | cut -d ',' -f 2 | tr -d ' %')

	energy=$(( ((load * 144) + 99) / 100 ))
	render "$(echo "$state" | tr '[:upper:]' '[:lower:]')" $energy
}

render() {
	state="$1"
	energy="$2"
	full=$(( (energy + 11) / 12 ))
	empty=$((12 - full))

	if [ "$state" = "charging" ]
	then
		colour=$yellow
	elif [ "$state" = "full" ] || [ "$state" = 'fully-charged' ]
	then
		colour=$green
	elif [ "$full" -le 1 ]
	then
		colour=$red
	else
		colour=$white
	fi

	printf "▕<span color='%s'>" "$colour"
	i=0; while [ $i -lt $empty ]
	do
		printf " "
		i=$(( i + 1 ))
	done
	i=0; while [ $i -lt "$full" ]
	do
		if [ $(( i % 2 )) -eq 0 ]
		then
			printf "█"
		else
			printf "▓"
		fi
		i=$(( i + 1 ))
	done

	printf "</span>"

}

if [ "$1" = '-1' ]
then
	if [ -e /tmp/swaybar/pipe ]
	then
		printf "battery|%s\n" "$(render_acpi)" >/tmp/swaybar/pipe
	fi
	exit
fi

battery=''
energy=-1
energyF=-1
energyE=-1
pc=0
pc_prev=0
state=""
state_prev=""
last_notify=""
upower --monitor-detail | while read -r line
do
	if echo "$line" | grep -qF '['
	then
		if echo "$line" | grep -q 'device changed'
		then
			battery=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 3)
		else
			battery=''
		fi
	fi

	if [ "$battery" = '/org/freedesktop/UPower/devices/battery_BAT1' ]
	then
		case $line in
		*state:*)
			state_prev=$state
			state=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 2)
			if [ "$state_prev" != "$state" ]
			then
				case $state in
					charging) notify-send -i battery-caution-charging-symbolic Battery charging ;;
					discharging) notify-send -i battery-good-symbolic Battery discharging ;;
					fully-charged) notify-send -i battery-full-charged-symbolic Battery full ;;
				esac
				last_notify="$state"
			fi
		;;
		*energy:*) energy=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 2);;
		*energy-empty:*) energyE=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 2);;
		*energy-full:*) energyF=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 2);;
		esac

		if [ "$energy" != '-1' ] && [ "$energyE" != '-1' ] && [ "$energyF" != '-1' ]
		then
			pc_prev=$pc
			pc=$(echo "2 k $energy $energyF $energyE - / 144 * p" | dc | jq '.|ceil')
		fi
		
		if [ "$pc_prev" != "$pc" ]
		then
			if [ "$pc" -le 3 ] && [ "$state" = 'discharging' ] && [ "$last_notify" != '3' ]
			then
				playerctl pause
				swaylock
		    echo mem > /sys/power/state
				last_notify='3'
			elif [ "$pc" -le 6 ] && [ "$state" = 'discharging' ] && [ "$last_notify" != '6' ]
			then
				notify-send -u critical -i battery-empty-symbolic Battery 'less than <sub>2</sub>6'
				say 'battery critically low'
				last_notify='6'
			elif [ "$pc" -le 12 ] && [ "$state" = 'discharging' ] && [ "$last_notify" != '12' ]
			then
				notify-send -i battery-low-symbolic Battery 'less than <sub>2</sub>10'
				last_notify='12'
			fi
			if [ -e /tmp/swaybar/pipe ]
			then
				printf "battery|%s\n" "$(render "$state" "$pc")" >/tmp/swaybar/pipe
			fi
		fi
	fi
done
